package com.example.administrator.mymusic.Class;

import android.os.Environment;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/4/14.
 */
public class LrcProcess {
    private List<LrcClass> listDatas;


    public List<LrcClass> readLrc(String name) {
        listDatas = new ArrayList<>();
        String musicLRC = Environment.getExternalStorageDirectory().getPath() + "/MyMusic/LRC/" + name + ".lrc";
        File file = new File(musicLRC);
        FileInputStream fileInputStream;
        InputStreamReader inputStreamReader;
        BufferedReader bufferedReader;
        try {
            fileInputStream = new FileInputStream(file);
            inputStreamReader = new InputStreamReader(fileInputStream, "GBK");
            bufferedReader = new BufferedReader(inputStreamReader);
            String s = "";
            while ((s = bufferedReader.readLine()) != null) {
//                00:00.00@ 徐良 - 北京巷弄
                s = s.trim();
                s = s.replace("[", "");
                s = s.replace("]", "@");
                String sp[] = s.split("@");
                if (sp.length > 1) {
                    listDatas.add(new LrcClass(LrcTime(sp[0]), sp[1]));
                }
            }
            bufferedReader.close();
            inputStreamReader.close();
            fileInputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
//            mWords.add("没有歌词文件，赶紧去下载");
        } catch (IOException e) {
            e.printStackTrace();
//            mWords.add("没有读取到歌词");
        }
//        finally {
//            try {
////                fileInputStream.close();
////                inputStreamReader.close();
////                bufferedReader.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }

//        }
        return listDatas;
    }

    public int LrcTime(String timeStr) {
//        00:00.00
        timeStr = timeStr.replace(":", ".");
        timeStr = timeStr.replace(".", "@");
        String timeData[] = timeStr.split("@");
        int minute = Integer.parseInt(timeData[0]);
        int second = Integer.parseInt(timeData[1]);
        int millisecond = Integer.parseInt(timeData[2]);
        int currentTime = (minute * 60 + second) * 1000 + millisecond * 10;
        return currentTime;
    }

}
